class LRUCache {
  constructor(maxSize) {
    this.maxSize = maxSize
    this.cacheStack = []
  }
  _update(index) {
    const cache = this.cacheStack.splice(index, 1)[0]
    this.cacheStack.unshift(cache)
  }
  get(key) {
    let cacheIndex = this.cacheStack.findIndex(item => item.key === key)
    if (cacheIndex === -1) return -1
    this._update(cacheIndex)
    return this.cacheStack[0].value
  }
  put(key, value) {
    let curSize = this.cacheStack.length
    if (curSize >= this.maxSize) {
      this.cacheStack.pop()
    }
    this.cacheStack.unshift({ key, value })
  }
} 

const cache = new LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
console.log(cache.get(1));
cache.put(3, 3)
console.log(cache.get(2))
cache.put(4, 4)
console.log(cache.get(1))
console.log(cache.get(3))
console.log(cache.get(4))

